package com.mgm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mgm.advertiser.service.AdvertiserService;
import com.mgm.exception.CustomException;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Advertiser;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;


/**
 * 广告主Controller
 * <p>Title: AdvertiserController</p>
 * <p>Description: </p>
 * <p>Company: MGM</p> 
 * @author	Andy
 * @date	2018年6月20日上午10:52:43
 * @version 1.0
 */
@Controller
@RequestMapping("/advertisers")
public class AdvertiserController {
	
	private static Logger logger =LoggerFactory.getLogger(AdvertiserController.class);

	@Autowired
	private AdvertiserService advertiserService;
	
	
	/**
	 * 
	 * @Title: listAdvertisers   
	 * @Description: 获取广告主列表 
	 * @param: @param pageNum
	 * @param: @param pageSize
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<DataGrid>>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<IResult<DataGrid>> listAdvertisers(@RequestParam(value="page", defaultValue="1") Integer pageNum,
			@RequestParam(value="ps", defaultValue="10") Integer pageSize, @RequestParam(value="account",required=false) String account,
			@RequestParam(value="adv_id",required=false) String adv_id, @RequestParam(value="name",required=false) String name,
			@RequestParam(value="enabled",required=false) String enabled, HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> map = new HashMap<>();
		map.put("account", account);
		map.put("adv_id", adv_id);
		map.put("name", name);
		map.put("enabled", enabled);
		DataGrid dg = advertiserService.findAll(map, pageNum, pageSize);
		IResult<DataGrid> result = IResult.success(dg);
		
		return new ResponseEntity<IResult<DataGrid>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: createAdvertiser  
	 * @Description: 添加广告主  
	 * @param: @param advertiser
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.POST) 
	public ResponseEntity<IMessage> createAdvertiser(@RequestBody Advertiser advertiser){
		List<Advertiser> list = advertiserService.findByName(advertiser.getName());
		if(!CollectionUtils.isEmpty(list)) {
			throw new CustomException("广告主名已存在");
		}
		IMessage msg= advertiserService.addAdvertiser(advertiser);
		return new ResponseEntity<IMessage>(msg, HttpStatus.CREATED);
	}
	
	/**
	 * 
	 * @Title: getAdvertiser   
	 * @Description: 获取广告主详情   
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<Advertiser>>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<IResult<Advertiser>> getAdvertiser(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		Advertiser advertiser = advertiserService.selectById(id);
		IResult<Advertiser> result = IResult.success(advertiser);
		return new ResponseEntity<IResult<Advertiser>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: updateAdvertiser   
	 * @Description: 更新广告主  
	 * @param: @param advertiser
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> updateAdvertiser(@RequestBody Advertiser advertiser){
		if(advertiser.getId()==null || advertiser.getId() <= 0) {
			logger.error("Illegal operation, Advertiser update fail!");
			throw new CustomException("非法操作，更新失败");
		}
		Advertiser u = advertiserService.selectById(advertiser.getId());
		if(u==null) {
			logger.error("Unable to update. Advertiser with id " + advertiser.getId() + " not found");
			throw new CustomException("广告主不存在，更新失败");
		}
		IMessage msg = advertiserService.updateAdvertiser(advertiser);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: deleteAdvertiser   
	 * @Description: 删除广告主
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
	public ResponseEntity<IMessage> deleteAdvertiser(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		Advertiser advertiser = advertiserService.selectById(id);
		if(advertiser==null) {
			logger.error("Unable to delete. Advertiser with id " + id + " not found");
			throw new CustomException("广告主不存在，删除失败");
		}
		IMessage msg= advertiserService.deleteAdvertiser(advertiser);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: enabled   
	 * @Description: 启用广告主   
	 * @param: @param id
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}/enabled", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> enabled(@PathVariable("id") Integer id, 
			@RequestBody Map<String, Boolean> map, 
			HttpServletRequest request, HttpServletResponse response) {
		Advertiser advertiser = advertiserService.selectById(id);
		if(advertiser==null) {
			logger.error("Unable to enabled. Advertiser with id " + id + " not found");
			throw new CustomException("广告主不存在，操作失败");
		}
		if(map==null || map.get("enabled")==null) {
			logger.error("[Enabled Advertiser] Illegal operation");
			throw new CustomException("非法操作");
		}
		
		IMessage msg= advertiserService.isEnabled(id, map.get("enabled"));
		
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: premium   
	 * @Description: 广告主溢价
	 * @param: @param id
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}/premium", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> premium(@PathVariable("id") Integer id, 
			@RequestBody Map<String, Integer> map, 
			HttpServletRequest request, HttpServletResponse response) {
		Advertiser advertiser = advertiserService.selectById(id);
		if(advertiser==null) {
			logger.error("Unable to enabled. Advertiser with id " + id + " not found");
			throw new CustomException("广告主不存在，操作失败");
		}
		if(map==null || map.get("premium")==null) {
			logger.error("[Premium Advertiser] Illegal operation");
			throw new CustomException("非法操作");
		}
		
		IMessage msg= advertiserService.modifyPremium(id, map.get("premium"));
		
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
}
